import ccxt



def get_ccxt_spot_symbols(exchange_id='binance'):
    """使用CCXT获取所有现货币对"""
    try:
        # 创建交易所实例
        # exchange = getattr(ccxt, exchange_id)({
        #     'sandbox': False,  # 使用实盘环境
        #     'verbose': False,  # 关闭详细日志
        # })
        # 设置代理的交易所配置
        exchange = ccxt.binance({
            'proxies': {
                'http': 'http://127.0.0.1:15236',  # HTTP代理
                'https': 'http://127.0.0.1:15236',  # HTTPS代理
                # 'http': 'socks5://127.0.0.1:1080',  # SOCKS5代理
                # 'https': 'socks5://127.0.0.1:1080', # SOCKS5代理
            },
            'verbose': False,  # 开启可以看到详细的请求信息
        })

        # 加载市场数据
        markets = exchange.load_markets()

        # 筛选现货交易对
        spot_symbols = []
        for symbol, market in markets.items():
            if market['spot'] and market['active']:
                spot_symbols.append(symbol)

        print(f"找到 {len(spot_symbols)} 个活跃的现货币对")
        return spot_symbols, exchange

    except Exception as e:
        print(f"错误: {e}")
        return [], None


# 使用示例
symbols, exchange = get_ccxt_spot_symbols('binance')
print(f"前10个交易对: {symbols[:10]}")